package com.example.spingbootmybatis.controller;

import com.example.spingbootmybatis.mapper.UserMapper;
import com.example.spingbootmybatis.pojo.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.net.HttpCookie;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class CommentController {
    @Autowired
    UserMapper mapper;

    @RequestMapping("/comment")
    public String comment(@RequestParam("id") Integer id,
                          @RequestParam("comment") String comment,
                          HttpSession session){
//        System.out.println("comment"+id);
//        System.out.println(comment);

        //使用Date获取当前时间
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式（年-月-日-时-分-秒）
        String createTime = dateFormat.format(now);//格式化然后放入字符串中

        //这条评论来自哪个用户
        String userwho = (String) session.getAttribute("userwho");
        String commentwho = "这条评论来自: " + userwho +" " + createTime;

        //获取过去的评论内容
        user user = mapper.queryUserById(id);
        String passcomment = user.getComment();

        //将这次评论的内容和过去别人评论的内容相加
        String sum = passcomment +"\n" + comment +"->" +commentwho;
        //进行更新数据库操作
        int i = mapper.updateComment(id,sum);
        System.out.println(i);
        return "dashboard";
    }
}
